#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin;

#info
function amh_module_info()
{
		echo 'AMH-ModuleName: Mongodb-2.0.4';
		echo 'AMH-ModuleDescription: Mongodb-2.0.4是dingyeyu开发的适配amh4.2的Nosql数据库模块';
		echo 'AMH-ModuleButton: 安装/卸载';
		echo 'AMH-ModuleDate: 2015-01-12';
		echo 'AMH-ModuleAdmin: /rockmongo';
		echo 'AMH-ModuleWebSite: http://git.oschina.net/dingyeyu/dingyeyu_amh/';
		echo 'AMH-ModuleIco: http://git.oschina.net/dingyeyu/dingyeyu_amh/raw/master/SoftWare/mongodb.jpg';
		echo 'AMH-ModuleScriptBy: dingyeyu';
}

#install
function amh_module_install()
{
	if amh_module_status ; then
		exit;
	else
		##安装mongodb
		cd /usr/local/;
		wget http://git.oschina.net/dingyeyu/dingyeyu_amh/raw/master/SoftWare/mongodb-linux-x86_64-2.0.4.tgz;
		tar -zxvf mongodb-linux-x86_64-2.0.4.tgz;
		mv mongodb-linux-x86_64-2.0.4 mongodb2.0.4;
		cd /home;
		mkdir mongodb;
		cd /home/mongodb;
		mkdir mongodb_db;
		touch mongodb_logs;
		ln /usr/local/mongodb2.0.4/bin/mongod /usr/bin/mongod;

		cd /root/amh/modules/Mongodb-2.0.4;
		cp -a ./rockmongo /home/wwwroot/index/web/;
		cp ./mongodb-init /etc/init.d/mongodb;
		chmod +x /etc/init.d/mongodb;
		useradd -s /sbin/nologin nobody;
		egrep -i "centos" /etc/issue && chkconfig --level 345 mongodb on || update-rc.d -f mongodb defaults;
		/etc/init.d/mongodb start;
		
		cd /usr/local/;
		wget http://git.oschina.net/dingyeyu/dingyeyu_amh/raw/master/SoftWare/mongo-1.5.8.tar.gz;
		tar -zxvf mongo-1.5.8.tar.gz;
		cd mongo-1.5.8;
		/usr/local/php/bin/phpize;
		./configure --with-php-config=/usr/local/php/bin/php-config;
		make && make install;
		
cat >> /etc/php.ini <<EOF

[mongo.so] 
extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/mongo.so
EOF
		amh php reload;

		amh_module_status;
	fi;
}

#admin 
function amh_module_admin()
{
	if amh_module_status ; then
		echo '[OK] Mongodb-2.0.4 Management: http://ip:8888/rockmongo ';
	else
		exit;
	fi;
}

#uninstall
function amh_module_uninstall()
{
	if amh_module_status ; then
		/etc/init.d/mongodb stop;
		rm -rf mongodb-linux-x86_64-2.0.4.tgz;
		rm -rf Mongodb-2.0.4;
		rm -rf /usr/local/mongodb2.0.4;
		rm -f /usr/bin/mongod /etc/init.d/mongodb;
		egrep -i "centos" /etc/issue && chkconfig mongodb off || update-rc.d -f mongodb remove;
		rm -rf /home/wwwroot/index/web/rockmongo;

		cd /usr/local/;
		rm -rf mongo-1.5.8;
		rm -rf mongo-1.5.8.tar.gz;
		rm /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/mongo.so;

		sed -i "/mongo\.so/d" /etc/php.ini;
		amh php reload;

		echo '[OK] Mongodb-2.0.4 Uninstall successful.';
	else
		exit;
	fi;
}

#status
function amh_module_status()
{
	if [ -f "/home/wwwroot/index/web/rockmongo/index.php" ]; then
		echo '[OK] Mongodb-2.0.4 is already installed.';
		return 0;
	else
		echo '[Notice] Mongodb-2.0.4 is not installed.';
		return 1;
	fi;
}